package phone.qyg.com.qyg.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.application.QXApplication;
import com.app.base.tool.BaseFragment;
import com.app.config.ApplicationConfig;
import com.app.utils.CommonUtil;
import com.app.utils.JSONTool;
import com.app.utils.OptionImageUtils;
import com.app.utils.PromptManager;
import com.app.utils.SharePrefUtil;
import com.app.utils.TransformController;
import com.app.view.CircleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.activity.HtmlActivity;
import phone.qyg.com.qyg.activity.LoginActivity;
import phone.qyg.com.qyg.activity.MsgActivity;
import phone.qyg.com.qyg.activity.MyCollectListActivity;
import phone.qyg.com.qyg.activity.MyCourseListActivity;
import phone.qyg.com.qyg.activity.MyNoteListActivity;
import phone.qyg.com.qyg.activity.OpinionActivity;
import phone.qyg.com.qyg.activity.SettingActivity;
import phone.qyg.com.qyg.activity.UpdateInfoActivity;
import phone.qyg.com.qyg.model.About;
import phone.qyg.com.qyg.model.UserInfo;
import phone.qyg.com.qyg.utils.AppHttpRequest;
import phone.qyg.com.qyg.utils.AppUpdateUtil;

public class MyFragment extends BaseFragment {
    @ViewInject(R.id.course_rela)
    LinearLayout course_rela;
    @ViewInject(R.id.collect_rela)
    LinearLayout collect_rela;
    @ViewInject(R.id.notes_rela)
    LinearLayout notes_rela;
    @ViewInject(R.id.history_rela)
    LinearLayout history_rela;
    @ViewInject(R.id.opinion_rela)
    LinearLayout opinion_rela;
    @ViewInject(R.id.update_rela)
    LinearLayout update_rela;
    @ViewInject(R.id.about_rela)
    LinearLayout about_rela;
    @ViewInject(R.id.setting)
    TextView setting;
    @ViewInject(R.id.subscribe_count)
    TextView subscribe_count;
    @ViewInject(R.id.course_count)
    TextView course_count;
    @ViewInject(R.id.tv_name)
    TextView tv_name;
    @ViewInject(R.id.iv_head)
    CircleImageView iv_head;
    @ViewInject(R.id.ll_user)
    LinearLayout ll_user;
    String appInfo;
    @Override
    public View initView(LayoutInflater inflater) {
        View view = View.inflate(QXApplication.getContext(), R.layout.fragment_my,null);
        x.view().inject(this,view);
        return view;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        appInfo();

        setState();
        isAppLogin();
    }
    private void appInfo(){
        appInfo = SharePrefUtil.getString(SharePrefUtil.KEY.APP_INFO,"");

        if(!CommonUtil.isRequestStr(appInfo)){
            AppHttpRequest.httpAppInfo(new AppHttpRequest.OnAppHttpListener() {
                @Override
                public void onAppHttpState(boolean isState, String result) {
                    if(isState){
                        appInfo = result;
                    }
                }
            });
        }
    }
    @Override
    public void onNetChange(int netMobile) {

    }
    @Event(value = {R.id.course_rela,R.id.collect_rela,R.id.notes_rela,R.id.history_rela,
    R.id.opinion_rela,R.id.update_rela,R.id.about_rela,R.id.setting,R.id.ll_user})
    private void viewOnClick(View v){
        String userInfo = SharePrefUtil.getString(SharePrefUtil.KEY.APP_USER, "");
        if (!isRequestStr(userInfo) && (!(v == setting || v == about_rela ||
                v == update_rela))) {
            //进行拦截 没有登录
            TransformController.transformController(QXApplication.getContext(), LoginActivity.class);
            return;
        }
        Class clazz = null;
        switch (v.getId()) {
            case R.id.course_rela:
                //课程
                clazz = MyCourseListActivity.class;
                break;
            case R.id.collect_rela:
                //收藏
                clazz = MyCollectListActivity.class;
                break;
            case R.id.notes_rela:
                //笔记
                clazz = MyNoteListActivity.class;
                break;
            case R.id.setting:
                clazz = SettingActivity.class;
                break;
            case R.id.history_rela:
                //消息
                clazz = MsgActivity.class;
                break;
            case R.id.opinion_rela:
                //意见
                clazz = OpinionActivity.class;
                break;
            case R.id.update_rela:
                //升级
                String appInfoValue = SharePrefUtil.getString(SharePrefUtil.KEY.APP_INFO, "");

                if (isRequestStr(appInfoValue)) {
                    String versionValue = JSONTool.jsonDefaluTranString(appInfoValue, JSONTool.Enum.APP_VERSION);
                    String lineVersion = JSONTool.requestJSONfindName(versionValue, JSONTool.Enum.LINE_VERSION);
                    if (CommonUtil.isRequestStr(lineVersion)) {
                        String appVersion = CommonUtil.getVersion();
                        String lochAppVersion = appVersion.replace(".", "0");
                        String lineAppVersion = lineVersion.replace(".", "0");
                        if (Integer.valueOf(lineAppVersion) > Integer.valueOf(lochAppVersion)) {
                            AppUpdateUtil.appUpdate("", update_rela);
                        } else {
                            PromptManager.showToast(R.string.app_version_hint);
                        }
                    }
                }
                break;
            case R.id.about_rela:
                //关于
                String url = JSONTool.jsonDefaluTranString(appInfo, JSONTool.Enum.ABOUT_US);
                About about = new About(setAttributeText(url),
                        QXApplication.getContext().getResources().getString(R.string.my_about));
                TransformController.transformControllerModel(QXApplication.getContext(), HtmlActivity.class, about);
                break;
            case R.id.ll_user:
                clazz = UpdateInfoActivity.class;
                break;
        }
        if(clazz != null)
            TransformController.transformController(QXApplication.getContext(),clazz);
    }

    private void setState(){
        String appInfo = SharePrefUtil.getString(SharePrefUtil.KEY.APP_USER,"");
        if(isRequestStr(appInfo)){
            UserInfo userInfo = JSONTool.requestJSONClazz(appInfo, UserInfo.class);
            subscribe_count.setText(String.valueOf(userInfo.actCount));
            course_count.setText(String.valueOf(userInfo.regCount));
            tv_name.setText(setAttributeText(userInfo.nick_name));
            ImageLoader.getInstance().displayImage(userInfo.fixImgUrl,iv_head,OptionImageUtils.getPersonIcon());
        }else{
            subscribe_count.setText("0");
            course_count.setText("0");
            tv_name.setText("登录/注册");
            iv_head.setImageResource(R.drawable.my_head);
        }
    }

    private void isAppLogin(){
        LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity());
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ApplicationConfig.APP_QX_LOGIN);
        BroadcastReceiver mItemViewListClickReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
//                intent.putExtra(TransformController.ChangeKEY.INTENT_KEY,result);
//                String result = intent.getStringExtra(TransformController.ChangeKEY.INTENT_KEY);
                setState();
            }
        };
        broadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter);
    }

}
